(function($){
	var tooltip_template = '<span class="_TYPE tooltip-content">_ICON_TITLE_CONTENT</span>',
		icon_template = '<img src="_ICON" class="tooltip-icon" alt="_TYPE" height="48" width="48" />',
		title_template = '<em class="tooltip-title">_TITLE</em>';
	
	$.fn.ADCTooltip = function(action){
		return $(this).each(function(){
			if(typeof action == 'object' || typeof action =='undefined'){
				//el = $(this);
				
				el = this;
				obj = {};
				obj['#'+el.id] = action||{};
				ADCTooltip(obj);
			}
			if (typeof action == 'string' && action == 'remove'){
				el = $(this);
				if(el.parent().hasClass('tooltip')){
					opts = el.data('ADCTooltip.options');
					el.parent().removeClass('tooltip').find('span[class^=tooltip]').remove();
					if(opts.useTitle){
						el.attr('title', opts.tip);
					}
					if(opts.wrap){
						el.unwrap();
					}
					el.removeData('ADCTooltip.options');
				}
			}
		});
	};
	
	ADCTooltip = function(options){
		//setting options for each input
		for(var i in options){
			el = $(i);
			opts = $.extend({}, ADCTooltip.defaults, options[i]);
			
			if(opts.useTitle){
				opts.tip = el.attr('title');
				el.removeAttr('title');
			}
			if(opts.wrap){
				el.wrap('<div class="tooltip" />');
			} else {
				el.parent().addClass('tooltip');
			}
			
			ttip=tooltip_template;
			icon = icon_template.replace('_TYPE', opts.type);
			switch(opts.type){
				case 'critical':
					opts.icon = opts.icon?opts.icon:_WEB_ROOT+'img/icons/tooltip/Critical.png';
					icon = icon.replace('_ICON', opts.icon);
					ttip = ttip.replace('_TYPE', 'tooltip-custom tooltip-critical');
					break;
				case 'information':
					opts.icon = opts.icon?opts.icon:_WEB_ROOT+'img/icons/tooltip/Info.png';
					icon = icon.replace('_ICON', opts.icon);
					ttip = ttip.replace('_TYPE', 'tooltip-custom tooltip-info');
					break;
				case 'help':
					opts.icon = opts.icon?opts.icon:_WEB_ROOT+'img/icons/tooltip/Help.png';
					icon = icon.replace('_ICON', opts.icon);
					ttip = ttip.replace('_TYPE', 'tooltip-custom tooltip-help');
					break;
				case 'warning':
					opts.icon = opts.icon?opts.icon:_WEB_ROOT+'img/icons/tooltip/Warning.png';
					icon = icon.replace('_ICON', opts.icon);
					ttip = ttip.replace('_TYPE', 'tooltip-custom tooltip-warning');
					break;
				case 'classic':
				default:
					if(opts.icon){
						icon = icon.replace('_ICON', opts.icon);
					} else {
						icon = '';
					}
					if(icon && opts.type!='classic'){
						ttip = ttip.replace('_TYPE', 'tooltip-custom '+ opts.type);
					} else {
						ttip = ttip.replace('_TYPE', 'tooltip-classic');
					}
			}
			
			title = '';
			if(opts.title){
				title = title_template.replace('_TITLE', opts.title);
			}
			ttip = ttip.replace('_ICON', icon).replace('_TITLE', title).replace('_CONTENT', opts.tip);
			el.data('ADCTooltip.options', opts);
			switch (opts.position){
				case 'before':
					el.before(ttip); break;
				case 'parent-append':
					el.parent().append(ttip); break;
				case 'parent-prepend':
					el.parent().prepend(ttip); break;
				case 'after':
				default:
					el.after(ttip); 
				
			}
		}
	};
	
	ADCTooltip.defaults = {
		useTitle:true, //false=>use "tip" property, true=>use object title as tip
		tip:'',
		title:'',
		wrap:true, //false => use parent else 'div'
		type:'classic', //critical information help warning
		icon:'', //each "type" has its own icon unless overriden by user
		position:'after' //before parent-append parent-prepend
	};
})(jQuery)